package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.HouseDTO;
import com.sanshi.project.admin.api.entity.CodeRuleEntity;
import com.sanshi.project.admin.api.entity.HouseEntity;
import com.sanshi.project.assets.service.CodeRuleService;
import com.sanshi.project.assets.service.HouseService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房屋资产
 *
 * @author nikon
 * @date 2023-11-18 14:11:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/house")
@Tag(description = "house", name = "房屋资产管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HouseController {

    private final HouseService houseService;
    private final CodeRuleService codeRuleService;

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param house 房屋资产
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_house_view')")
    public R getHousePage(@ParameterObject Page page, @ParameterObject HouseDTO house) {
        LambdaQueryWrapper<HouseEntity> wrapper = Wrappers.lambdaQuery();
        // 房屋编码
        wrapper.like(StrUtil.isNotBlank(house.getCode()), HouseEntity::getCode, house.getCode());
        // 房屋名称
        wrapper.like(StrUtil.isNotBlank(house.getName()), HouseEntity::getName, house.getName());
        // 项目类型
        wrapper.in(ArrayUtil.isNotEmpty(house.getType()), HouseEntity::getType, house.getType());
        // 项目
        wrapper.in(ArrayUtil.isNotEmpty(house.getProjectId()), HouseEntity::getProjectId, house.getProjectId());
        // 楼栋
        wrapper.in(ArrayUtil.isNotEmpty(house.getBuildingId()), HouseEntity::getBuildingId, house.getBuildingId());
        // 产权归属
        wrapper.in(ArrayUtil.isNotEmpty(house.getOwnership()), HouseEntity::getOwnership, house.getOwnership());
        // 产权单位
        wrapper.in(ArrayUtil.isNotEmpty(house.getUnit()), HouseEntity::getUnit, house.getUnit());
        // 建筑面积
        if (ArrayUtil.isNotEmpty(house.getFloorArea()))
            wrapper.ge(HouseEntity::getFloorArea, house.getFloorArea()[0]).le(HouseEntity::getFloorArea, house.getFloorArea()[1]);
        // 使用面积
        if (ArrayUtil.isNotEmpty(house.getUseArea()))
            wrapper.ge(HouseEntity::getUseArea, house.getUseArea()[0]).le(HouseEntity::getUseArea, house.getUseArea()[1]);
        // 房型
        wrapper.in(ArrayUtil.isNotEmpty(house.getLayout()), HouseEntity::getLayout, house.getLayout());
        // 朝向
        wrapper.in(ArrayUtil.isNotEmpty(house.getOrien()), HouseEntity::getOrien, house.getOrien());
        // 装修程度
        wrapper.in(ArrayUtil.isNotEmpty(house.getRenovation()), HouseEntity::getRenovation, house.getRenovation());
        // 房屋用途
        wrapper.in(ArrayUtil.isNotEmpty(house.getPurpose()), HouseEntity::getPurpose, house.getPurpose());
        // 房屋商圈
        wrapper.in(ArrayUtil.isNotEmpty(house.getBusinessDistrict()), HouseEntity::getBusinessDistrict, house.getBusinessDistrict());
        // 房屋添加方式
        wrapper.in(ArrayUtil.isNotEmpty(house.getAddType()), HouseEntity::getAddType, house.getAddType());
        // 是否
        wrapper.in(ArrayUtil.isNotEmpty(house.getIsCard()), HouseEntity::getIsCard, house.getIsCard());
        // 房屋状态
        wrapper.in(ArrayUtil.isNotEmpty(house.getStatus()), HouseEntity::getStatus, house.getStatus());
        wrapper.orderByDesc(HouseEntity::getUpdateTime);
        return R.ok(houseService.page(page, wrapper));
    }


    /**
     * 通过id查询房屋资产
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_house_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(houseService.getById(id));
    }

    /**
     * 新增房屋资产
     *
     * @param house 房屋资产
     * @return R
     */
    @Operation(summary = "新增房屋资产", description = "新增房屋资产")
    @SysLog("新增房屋资产")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_house_add')")
    public R save(@RequestBody HouseEntity house) {
        CodeRuleEntity codeRuleEntity = new CodeRuleEntity();
        codeRuleEntity.setType("30");
        R<String> resultCode = codeRuleService.generateCode(codeRuleEntity);
        if (resultCode.isOk()) {
            // 赋予房屋编码
            house.setCode(resultCode.getData());
        } else {
            return resultCode;
        }
        return R.ok(houseService.save(house));
    }

    /**
     * 修改房屋资产
     *
     * @param house 房屋资产
     * @return R
     */
    @Operation(summary = "修改房屋资产", description = "修改房屋资产")
    @SysLog("修改房屋资产")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_house_edit')")
    public R updateById(@RequestBody HouseEntity house) {
        return R.ok(houseService.updateById(house));
    }

    /**
     * 通过id删除房屋资产
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除房屋资产", description = "通过id删除房屋资产")
    @SysLog("通过id删除房屋资产")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_house_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(houseService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @param house 查询条件
     * @param ids   导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_house_export')")
    public List<HouseEntity> export(HouseEntity house, Long[] ids) {
        return houseService.list(Wrappers.lambdaQuery(house).in(ArrayUtil.isNotEmpty(ids), HouseEntity::getId, ids));
    }

    /**
     * 房屋资产全量查询
     *
     * @param house 房屋资产管理
     * @return
     */
    @Operation(summary = "房屋资产管理查询", description = "房屋资产管理查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('assets_house_view')")
    public R getHouseList(@ParameterObject HouseDTO house) {
        LambdaQueryWrapper<HouseEntity> wrapper = Wrappers.lambdaQuery();
        // 房屋编码
        wrapper.like(StrUtil.isNotBlank(house.getCode()), HouseEntity::getCode, house.getCode());
        // 房屋名称
        wrapper.like(StrUtil.isNotBlank(house.getName()), HouseEntity::getName, house.getName());
        // 项目类型
        wrapper.in(ArrayUtil.isNotEmpty(house.getType()), HouseEntity::getType, house.getType());
        // 项目
        wrapper.in(ArrayUtil.isNotEmpty(house.getProjectId()), HouseEntity::getProjectId, house.getProjectId());
        // 楼栋
        wrapper.in(ArrayUtil.isNotEmpty(house.getBuildingId()), HouseEntity::getBuildingId, house.getBuildingId());
        // 产权归属
        wrapper.in(ArrayUtil.isNotEmpty(house.getOwnership()), HouseEntity::getOwnership, house.getOwnership());
        // 产权单位
        wrapper.in(ArrayUtil.isNotEmpty(house.getUnit()), HouseEntity::getUnit, house.getUnit());
        // 建筑面积
        if (ArrayUtil.isNotEmpty(house.getFloorArea()))
            wrapper.ge(HouseEntity::getFloorArea, house.getFloorArea()[0]).le(HouseEntity::getFloorArea, house.getFloorArea()[1]);
        // 使用面积
        if (ArrayUtil.isNotEmpty(house.getUseArea()))
            wrapper.ge(HouseEntity::getUseArea, house.getUseArea()[0]).le(HouseEntity::getUseArea, house.getUseArea()[1]);
        // 房型
        wrapper.in(ArrayUtil.isNotEmpty(house.getLayout()), HouseEntity::getLayout, house.getLayout());
        // 朝向
        wrapper.in(ArrayUtil.isNotEmpty(house.getOrien()), HouseEntity::getOrien, house.getOrien());
        // 装修程度
        wrapper.in(ArrayUtil.isNotEmpty(house.getRenovation()), HouseEntity::getRenovation, house.getRenovation());
        // 房屋用途
        wrapper.in(ArrayUtil.isNotEmpty(house.getPurpose()), HouseEntity::getPurpose, house.getPurpose());
        // 房屋商圈
        wrapper.in(ArrayUtil.isNotEmpty(house.getBusinessDistrict()), HouseEntity::getBusinessDistrict, house.getBusinessDistrict());
        // 房屋添加方式
        wrapper.in(ArrayUtil.isNotEmpty(house.getAddType()), HouseEntity::getAddType, house.getAddType());
        // 是否
        wrapper.in(ArrayUtil.isNotEmpty(house.getIsCard()), HouseEntity::getIsCard, house.getIsCard());
        // 房屋状态
        wrapper.in(ArrayUtil.isNotEmpty(house.getStatus()), HouseEntity::getStatus, house.getStatus());
        wrapper.orderByDesc(HouseEntity::getUpdateTime);
        return R.ok(houseService.list(wrapper));
    }
}